using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Net;
using UnityEditor.Build;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.Networking;

namespace RichFrame
{
    /// <summary>
    /// 异步资源请求
    /// </summary>
    public class AssetRequest
    {
        /// <summary>
        /// 获取资源
        /// </summary>
        public UnityEngine.Object Asset
        {
            get
            {
                return getAsset();
            }
        }
        /// <summary>
        /// 资源加载进度
        /// </summary>
        public float Progress
        {
            get
            {
                if (getProgress != null)
                    return getProgress();
                else
                {
                    if (Asset != null)
                    {
                        return 1;
                    }
                    else
                    {
                        return 0;
                    }
                }
            }
        }
        /// <summary>
        /// 是否加载完成
        /// </summary>
        public bool IsDone
        { 
            get 
            {
                return isDone();
            }
        }
        object enumerator; 
        Func<bool> isDone;
        Func<float> getProgress;
        Func<UnityEngine.Object> getAsset;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="enumerator">迭代器</param>
        /// <param name="getAsset">获取资源函数</param>
        /// <param name="isDone">获取是否完成函数</param>
        /// <param name="getProgress">获取进度函数</param>
        /// <exception cref="Exception"></exception>
        internal AssetRequest([NotNull] object enumerator, [NotNull] Func<UnityEngine.Object> getAsset, [NotNull] Func<bool> isDone, Func<float> getProgress)
        {
            if (getAsset == null)
            {
                throw new Exception("getAsset can't be null!");
            }
            if (enumerator == null)
            {
                throw new Exception("enumerator can't be null!");
            }
            if (isDone == null)
            {
                throw new Exception("isDone can't be null!");
            }
            this.enumerator = enumerator;
            this.getAsset = getAsset;
            this.getProgress = getProgress;
            this.isDone = isDone;
        }
        /// <summary>
        /// 获取迭代器
        /// </summary>
        /// <returns></returns>
        public IEnumerator GetEnumerator()
        {
            yield return enumerator;
        }
    }
}
